package com.tz.spring.sysmanage.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.tz.spring.sysmanage.dao.IStudentDao;
import com.tz.spring.sysmanage.entity.Student;

public class StudentDaoOther extends JdbcDaoSupport implements IStudentDao{

	 
	@Override
	public boolean addStudent(Student student) {
		boolean flag = false;
		String addSql = "INSERT INTO STUDENT(NAME,AGE)VALUES(?,?) ";
		int rows = this.getJdbcTemplate().update(addSql, student.getName(),student.getAge());
		if(rows>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean delStudent(Integer studentId) {
		boolean flag = false;
		String delSql = "DELETE FROM STUDENT WHERE ID = ?";
		int rows = this.getJdbcTemplate().update(delSql,studentId);
		if(rows>0){
			flag = true;
		}		
		return flag;
	}

	@Override
	public boolean updateStudent(Student student) {
		boolean flag = false;
		String updateSql = "UPDATE STUDENT SET NAME= ? ,AGE = ? WHERE ID = ?";
		int rows = this.getJdbcTemplate().update(updateSql,student.getName(),student.getAge(),student.getId());
		if(rows>0){
			flag = true;
		}		
		return flag;
	}

	@Override
	public List<Student> getStudentList() {
 		String querySql = "SELECT ID,NAME,AGE FROM STUDENT ";
		//第一种方式 用接口的匿名内部类实现
 		/*return  this.jdbcTemplate.query(querySql, new RowMapper<Student>(){
			@Override
			public Student mapRow(ResultSet resultSet, int rowNum) throws SQLException {
				Student stu  = new Student();
				stu.setId(resultSet.getInt("ID"));
				stu.setName(resultSet.getString("NAME"));
				stu.setAge(resultSet.getInt("AGE"));
				return stu;
			}			
		});*/
 		//第二种方式,直接将行结果集映射到实体类
 		return this.getJdbcTemplate().query(querySql, 
 						new BeanPropertyRowMapper<Student>(Student.class));
		
		
	}
	
	@Override
	public List<Map<String, Object>> getStudentMap() {
		return  this.getJdbcTemplate().queryForList("SELECT ID,NAME,AGE FROM STUDENT");
		
	}

	  
}
